﻿namespace Vdc.Grid
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class GridRow : TableRow, IDataItemContainer, INamingContainer
    {
        private object dataItem;
        private int dataItemIndex;
        private int groupLevel;
        private int rowIndex;
        private DataControlRowState rowState;
        private GridRowType rowType;

        public GridRow(int rowIndex, int dataItemIndex, GridRowType rowType, DataControlRowState rowState)
        {
            this.rowIndex = rowIndex;
            this.dataItemIndex = dataItemIndex;
            this.rowType = rowType;
            this.rowState = rowState;
            this.TableSection = TableRowSection.TableBody;
        }

        internal object GetCellData(string columnName)
        {
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(this.dataItem))
            {
                if (descriptor.Name.ToLower() == columnName.ToLower())
                {
                    return descriptor.GetValue(this.dataItem);
                }
            }
            return null;
        }

        public virtual Hashtable ToHashtable()
        {
            Hashtable hashtable = new Hashtable();
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(this.dataItem))
            {
                hashtable.Add(descriptor.Name, descriptor.GetValue(this.dataItem).ToString());
            }
            return hashtable;
        }

        public virtual object DataItem
        {
            get
            {
                return this.dataItem;
            }
            set
            {
                this.dataItem = value;
            }
        }

        public virtual int DataItemIndex
        {
            get
            {
                return this.dataItemIndex;
            }
        }

        public virtual int GroupLevel
        {
            get
            {
                return this.groupLevel;
            }
            set
            {
                this.groupLevel = value;
            }
        }

        public virtual int RowIndex
        {
            get
            {
                return this.rowIndex;
            }
        }

        public virtual DataControlRowState RowState
        {
            get
            {
                return this.rowState;
            }
            set
            {
                this.rowState = value;
            }
        }

        public virtual GridRowType RowType
        {
            get
            {
                return this.rowType;
            }
            set
            {
                this.rowType = value;
            }
        }

        object IDataItemContainer.DataItem
        {
            get
            {
                return this.DataItem;
            }
        }

        int IDataItemContainer.DataItemIndex
        {
            get
            {
                return this.dataItemIndex;
            }
        }

        int IDataItemContainer.DisplayIndex
        {
            get
            {
                return this.RowIndex;
            }
        }
    }
}

